package com.parktech.sun.domain.service.industryservice.impl;

import com.parktech.sun.domain.model.industryservice.IndustryService;
import com.parktech.sun.domain.model.industryservice.IndustryServiceInfoCategory;
import com.parktech.sun.domain.model.industryservice.IndustryServiceInformation;
import com.parktech.sun.domain.repository.Page;
import com.parktech.sun.domain.repository.industryservice.IndustryServiceRepository;
import com.parktech.sun.domain.service.impl.BaseServiceImpl;
import com.parktech.sun.domain.service.industryservice.IndustryServiceService;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.*;
import org.hibernate.mapping.Join;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * User: Think
 * Date: 11-1-12
 * Time: 下午10:50
 */
@Service
public class IndustryServiceServiceImpl extends BaseServiceImpl<IndustryService> implements IndustryServiceService {

    private IndustryServiceRepository industryServiceRepository;

    @Autowired
    public IndustryServiceServiceImpl(IndustryServiceRepository industryServiceRepository) {
        super(industryServiceRepository);
        this.industryServiceRepository = industryServiceRepository;
    }

    public Page search(String title, Page page) {

        Criteria criteria = industryServiceRepository.createCriteria();

        if (StringUtils.isNotEmpty(title)) {
            criteria.add(Property.forName("title").like(title, MatchMode.ANYWHERE));
        }

        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("publishDate"));
        criteria.addOrder(Order.desc("modifyTime"));

        return industryServiceRepository.page(criteria, page);

    }

    public Page searchInformation(String title, Long category, Integer publishStatus, Page page) {

        Criteria criteria = industryServiceRepository.createCriteria(IndustryServiceInformation.class);

        if (StringUtils.isNotEmpty(title)) {
            criteria.add(Property.forName("title").like(title, MatchMode.ANYWHERE));
        }
        if (category != null && category != -1L) {
            criteria.add(Property.forName("industryService.id").eq(category));
        }

        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("publishDate"));
        criteria.addOrder(Order.desc("modifyTime"));

        return industryServiceRepository.page(criteria, page);
    }

    public IndustryServiceInformation getInformationById(Long industryServiceInformationId) {
        return industryServiceRepository.getInformationById(industryServiceInformationId);
    }

    public void clearThenSaveOrUpdateInformation(IndustryServiceInformation industryServiceInformation) {
        industryServiceRepository.clearThenSaveOrUpdateInformation(industryServiceInformation);
    }

    public void saveOrUpdateInformation(IndustryServiceInformation industryServiceInformation) {
        industryServiceRepository.saveOrUpdateInformation(industryServiceInformation);
    }

    public Page searchCategory(String title, Long category, Page page) {

        Criteria criteria = industryServiceRepository.createCriteria(IndustryServiceInfoCategory.class);

        if (StringUtils.isNotEmpty(title)) {
            criteria.add(Property.forName("name").like(title, MatchMode.ANYWHERE));
        }
        if (category != null && category != -1L) {
            criteria.add(Property.forName("industryService.id").eq(category));
        }

        return industryServiceRepository.page(criteria, page);
    }

    public void clearThenSaveOrUpdateCategory(IndustryServiceInfoCategory industryCategory) {
        industryServiceRepository.clearThenSaveOrUpdateCategory(industryCategory);
    }

    public IndustryServiceInfoCategory getByCategoryId(Long aLong) {
        return industryServiceRepository.getByCategoryId(aLong);
    }

    public void saveOrUpdateCategory(IndustryServiceInfoCategory industryCategory) {
        industryServiceRepository.saveOrUpdateCategory(industryCategory);
    }

    public List<IndustryServiceInfoCategory> getWithSubCategoryAndTop4Info(Long industryServiceId) {

        Criteria criteria = industryServiceRepository.createCriteria(IndustryServiceInfoCategory.class);

        if (industryServiceId != null && industryServiceId != -1L) {
            criteria.add(Property.forName("industryService.id").eq(industryServiceId));
        }
        criteria.addOrder(Order.desc("orderCode"));
        List<IndustryServiceInfoCategory> categoryList = criteria.list();
        for (IndustryServiceInfoCategory industryServiceInfoCategory : categoryList) {
            getTop4Information(industryServiceInfoCategory);
        }
        return categoryList;
    }

    private void getTop4Information(final IndustryServiceInfoCategory industryServiceInfoCategory) {
        Criteria infoCriteria = industryServiceRepository.createCriteria(IndustryServiceInformation.class);
        infoCriteria.add(Property.forName("category.id").eq(industryServiceInfoCategory.getId()));
        infoCriteria.add(Property.forName("publishStatus").eq(1));
        infoCriteria.addOrder(Order.desc("orderCode"));
        infoCriteria.addOrder(Order.desc("publishDate"));
        infoCriteria.setMaxResults(4);
        industryServiceInfoCategory.setInformationList(infoCriteria.list());
    }

    public Page pageInfoBy(Long industryServiceId, Long subCategoryId, Page page) {

        Criteria criteria = industryServiceRepository.createCriteria(IndustryServiceInformation.class);

        if (industryServiceId != null && industryServiceId != -1L) {
            criteria.add(Property.forName("industryService.id").eq(industryServiceId));
        }
        if (subCategoryId != null && subCategoryId != -1L) {
            criteria.add(Property.forName("category.id").eq(subCategoryId));
        }
        criteria.add(Property.forName("publishStatus").eq(1));
        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("publishDate"));

        return industryServiceRepository.page(criteria, page);
    }

    public List<IndustryServiceInformation> listTop7PolicyInfo() {

/*
        Criteria criteria = industryServiceRepository.createCriteria(IndustryServiceInformation.class);
        criteria.createAlias("cat", "category");
        criteria.createAlias("cat.name", "category.name");
//        criteria.add(Expression.eqProperty("category.name", "产业政策"));
        criteria.add(Property.forName("cat.name").eq("产业政策"));
        criteria.add(Property.forName("publishStatus").eq(1));
        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("publishDate"));
        criteria.setMaxResults(7);

        return criteria.list();
*/
        return industryServiceRepository.listTop7PolicyInfo();
    }

    public List<IndustryServiceInformation> listTop6RequirementInfo() {

/*
        Criteria criteria = industryServiceRepository.createCriteria(IndustryServiceInformation.class);
        criteria.add(Property.forName("category.name").eq("产业需求"));
        criteria.add(Property.forName("publishStatus").eq(1));
        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("publishDate"));
        criteria.setMaxResults(6);

        return criteria.list();
*/
        return industryServiceRepository.listTop6RequirementInfo();
    }

    public Page pageByPolicy(Page page) {
        Criteria criteria = industryServiceRepository.createCriteria(IndustryServiceInformation.class);
        criteria.createAlias("category", "category", CriteriaSpecification.LEFT_JOIN);
//        criteria.add(Expression.eqProperty("category.name", "产业政策"));
        criteria.add(Property.forName("category.name").eq("产业政策"));
        criteria.add(Property.forName("publishStatus").eq(1));
        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("publishDate"));

        return industryServiceRepository.page(criteria, page);
    }

    public Page pageByRequirement(Page page) {

        Criteria criteria = industryServiceRepository.createCriteria(IndustryServiceInformation.class);
        criteria.createAlias("category", "category", CriteriaSpecification.LEFT_JOIN);
//        criteria.add(Expression.eqProperty("category.name", "产业政策"));
        criteria.add(Property.forName("category.name").eq("产业需求"));
        criteria.add(Property.forName("publishStatus").eq(1));
        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("publishDate"));

        return industryServiceRepository.page(criteria, page);
    }
}
